package us.feng.tinyspring.beans;

import java.util.ArrayList;
import java.util.List;


/**
 * 默认BeanDefinition
 * 
 * @author feng
 *
 */
public class GenericBeanDefinition implements BeanDefinition{

	private PropertyValues propertyValues=new InternalPropertyValues();
	
	private String beanClassName;
	
	@Override
	public String getBeanClassName() {
		return beanClassName;
	}
	
	public void setBeanClassName(String beanClassName){
		this.beanClassName=beanClassName;
	}

	@Override
	public PropertyValues getPropertyValues() {
		
		return propertyValues;
	}

	private static class InternalPropertyValues implements PropertyValues{
		private final List<PropertyValue> propertyValueList=new ArrayList<PropertyValue>();
		
		@Override
		public PropertyValue[] getPropertyValues() {
			return propertyValueList.toArray(new PropertyValue[this.propertyValueList.size()]);
		}

		@Override
		public PropertyValue getPropertyValue(String propertyName) {
			for(PropertyValue pv:propertyValueList){
				if(pv.getName().equals(propertyName)){
					return pv;
				}
			}
			return null;
		}

		@Override
		public PropertyValues addPropertyValue(PropertyValue pv) {
			for (int i = 0; i < this.propertyValueList.size(); i++) {
				PropertyValue currentPv = this.propertyValueList.get(i);
				if (currentPv.getName().equals(pv.getName())) {
					return this;
				}
			}
			this.propertyValueList.add(pv);
			return this;
		}

		@Override
		public void removePropertyValue(PropertyValue pv) {
			this.propertyValueList.remove(pv);
		}

	}
}
